package com.example.pluginapp;

import org.pf4j.DefaultPluginManager;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.pf4j.ZipPluginManager;
import org.pf4j.demo.api.IPf4jGreeting;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

@SpringBootApplication
public class PluginAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(PluginAppApplication.class, args);

//        PluginManager pluginManager = new DefaultPluginManager();
//        pluginManager.loadPlugins();
//        pluginManager.startPlugins();
//
//        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
//        greetings.forEach(IPf4jGreeting::sayHello);
//
//        pluginManager.stopPlugins();

        // todo jar包一个类会扫出两个对象，导致执行了两遍，需要解决、、、、
        testJar();
        System.out.println("----------------------------------------------------------------------");
        testZip();


    }

    private static void testJar(){
        String path = PluginAppApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println("path: " + path);
        // 加载指定路径插件
//        pluginManager.loadPlugin(Paths.get("E:\\workspaceidea\\pf4jdemo\\plugin1-1.0-SNAPSHOT.jar"));
        String jarPath = "";
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equals("linux")) {

        } else {
            jarPath = path.substring(1) + "plugin1-1.0-SNAPSHOT.jar";
        }
        System.out.println("jarPath: " + jarPath);

        PluginManager pluginManager = new JarPluginManager();
        pluginManager.loadPlugin(Paths.get(jarPath));
        // 启动指定插件(也可以加载所有插件)
        pluginManager.startPlugin("welcome-plugin");
        // 执行插件
        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
        for (IPf4jGreeting greeting : greetings) {
            greeting.sayHello();
        }
        // 停止并卸载指定插件
        pluginManager.stopPlugin("welcome-plugin");
        pluginManager.unloadPlugin("welcome-plugin");
    }

    private static void testZip(){
        String path = PluginAppApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println("path: " + path);
        // 加载指定路径插件
//        pluginManager.loadPlugin(Paths.get("E:\\workspaceidea\\pf4jdemo\\zip\\plugin1-1.0-SNAPSHOT.jar.zip"));
        String zipPath = "";
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equals("linux")) {

        } else {
            zipPath = path.substring(1) + "zip" + File.separator + "plugin1-1.0-SNAPSHOT.jar.zip";
        }
        System.out.println("jarPath: " + zipPath);

        PluginManager pluginManager = new ZipPluginManager();
        // 加载指定路径插件
        pluginManager.loadPlugin(Paths.get(zipPath));
        // 启动指定插件(也可以加载所有插件)
        pluginManager.startPlugin("welcome-plugin");
        // 执行插件
        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
        for (IPf4jGreeting greeting : greetings) {
            greeting.sayHello();
        }
        // 停止并卸载指定插件
        pluginManager.stopPlugin("welcome-plugin");
        pluginManager.unloadPlugin("welcome-plugin");
    }


}
